﻿using System;
using System.Linq;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Data.Services.Client;

// Author: Miroslav Matula m.matula@alutam.cz 
// Date : 11.8.2010

namespace Silverlight.Test
{
  // Testování CRUD operací OData Service.

  // Metody se vykonávají v abecedním pořadí. 
  // Protože v tomto testu záleží na pořadí v jakém se jednotlivé metody vykonávají, proto názvy metod začínají čísly.

  // Aby bylo možno používat v test asynchronní volání, musí class dědit z  Microsoft.Silverlight.Testing.SilverlightTest
  //[TestClass]
  public class CustomServiceTests:Microsoft.Silverlight.Testing.SilverlightTest
  {
    public CustomServiceTests()
    {
      _proxy = new CustomService.CustomContext(new Uri(_uristr));
      _proxy.MergeOption = MergeOption.AppendOnly;
    }
    private const string _uristr = @"http://localhost:53738/Samples/CustomService.svc";
    private CustomService.CustomContext _proxy;
    private Guid _productID;
    private CustomService.Product _product; 

    [TestMethod]
    [Asynchronous]
    [Description("Kontrola počtu entit")]
    public void T01_ProdutsCount() 
    {
      // Dotaz lze generovat přes LINQ
      // var query = _proxy.Products.IncludeTotalCount().Take(0) as DataServiceQuery<CustomService.Product>;
      // string suri = query.ToString();

      // Nebo zapsat přímo dotaz
      string suri = _uristr + @"/Products()?$top=0&$inlinecount=allpages";

      _proxy.BeginExecute<CustomService.Product>(new Uri(suri), (arg) => {
        var res = _proxy.EndExecute<CustomService.Product>(arg) as QueryOperationResponse<CustomService.Product>;
        Assert.IsTrue(arg.IsCompleted);
        Assert.IsTrue(res.TotalCount == 295);
        TestComplete();
      }, null);
    }

    [TestMethod]
    [Asynchronous]
    [Description("Přidání Entity")]
    public void T02_AddProduct() 
    {
      var item = new CustomService.Product() {
        ProductID = Guid.NewGuid(),
        ProductNumber="test nr.",
        Name ="test product",
        Color = "silver",
        ListPrice = 10.12M,
        StandardCost = 15M,
        Size = 44,
        Weight = 0.25M,
        SellStartDate = DateTime.Now.Date
      };
      // ulož ProductID  pro následující test
      _productID = item.ProductID;
      _proxy.AddToProducts(item);

      _proxy.BeginSaveChanges(arg => {
        _proxy.EndSaveChanges(arg);
        Assert.IsTrue(arg.IsCompleted);
        TestComplete();
      }, null);
    }

    [TestMethod]
    [Asynchronous]
    [Description("Kontrola počtu entit")]
    public void T03_ProdutsCount()
    {
      string suri = _uristr + @"/Products()?$top=0&$inlinecount=allpages";

      _proxy.BeginExecute<CustomService.Product>(new Uri(suri), (arg) => {
        var res = _proxy.EndExecute<CustomService.Product>(arg) as QueryOperationResponse<CustomService.Product>;
        Assert.IsTrue(arg.IsCompleted);
        Assert.IsTrue(res.TotalCount == 296); // 295+1 Přidaná jedna entita
        TestComplete();
      }, null);
    }

    [TestMethod]
    [Asynchronous]
    [Description("Čte konrétní product")]
    public void T04_GetProdut()
    {
      var query = (from p in _proxy.Products where p.ProductID==_productID select p)  as DataServiceQuery<CustomService.Product>;
      string suri = query.ToString();

      _proxy.BeginExecute<CustomService.Product>(new Uri(suri), (arg) => {
        var res = _proxy.EndExecute<CustomService.Product>(arg) as QueryOperationResponse<CustomService.Product>;
        Assert.IsTrue(arg.IsCompleted);
        _product = res.First();
        Assert.AreEqual(_productID, _product.ProductID);
        TestComplete();
      }, null);
    }

    [TestMethod]
    [Asynchronous]
    [Description("Update Product")]
    public void T05_UpdateProduct()
    {
      _product.ListPrice = 9.99M;
      _proxy.UpdateObject(_product);
      _proxy.BeginSaveChanges(arg => {
        _proxy.EndSaveChanges(arg);
        Assert.IsTrue(arg.IsCompleted);
        TestComplete();
      }, null);
    }

    [TestMethod]
    [Asynchronous]
    [Description("Čte konrétní product")]
    public void T06_GetProdut()
    {
      var query = (from p in _proxy.Products where p.ProductID == _productID select p) as DataServiceQuery<CustomService.Product>;
      string suri = query.ToString();

      _proxy.BeginExecute<CustomService.Product>(new Uri(suri), (arg) => {
        var res = _proxy.EndExecute<CustomService.Product>(arg) as QueryOperationResponse<CustomService.Product>;
        Assert.IsTrue(arg.IsCompleted);
        _product = res.First();
        Assert.AreEqual(9.99M, _product.ListPrice);
        TestComplete();
      }, null);
    }

    [TestMethod]
    [Asynchronous]
    [Description("Delete Product")]
    public void T07_DeleteProduct()
    {
      _proxy.DeleteObject(_product);
      _proxy.BeginSaveChanges(arg => {
        _proxy.EndSaveChanges(arg);
        Assert.IsTrue(arg.IsCompleted);
        TestComplete();
      }, null);
    }

    [TestMethod]
    [Asynchronous]
    [Description("Kontrola počtu entit")]
    public void T08_ProdutsCount()
    {
      string suri = _uristr + @"/Products()?$top=0&$inlinecount=allpages";

      _proxy.BeginExecute<CustomService.Product>(new Uri(suri), (arg) => {
        var res = _proxy.EndExecute<CustomService.Product>(arg) as QueryOperationResponse<CustomService.Product>;
        Assert.IsTrue(arg.IsCompleted);
        Assert.IsTrue(res.TotalCount == 295); // Původní množství
        TestComplete();
      }, null);
    }

  }
}